<template>
  <div class="collect">
    <!-- Add a static page for my favorite module -->
    <div class="collect-header">
      <div class="collect-title">
        <i class="el-icon-collection-tag" style="color: #ff6700;"></i>
        我的收藏
      </div>
    </div>
    <div class="content">
      <ul class="goods-list" v-if="collectList.length>0">
        <li v-for="(itme,key) in collectList" :key="key" @mouseover="delet(key)">
          <i
            class="el-icon-close delete"
            slot="reference"
            v-show="isDelete==key"
            @click="deleteCollect(itme.product_id)"
          ></i>
          <img :src="'http://39.100.7.70:81/'+itme.product_picture" alt />
          <p>{{itme.product_name}}</p>
          <p>{{itme.product_title}}</p>
          <p>
            <span>{{itme.product_selling_price}}元</span>
            <s>{{itme.product_price}}元</s>
          </p>
        </li>
      </ul>
      <!-- 收藏列表为空的时候显示的内容 -->
      <div v-else class="collect-empty">
        <div class="empty">
          <h2>您的收藏还是空的！</h2>
          <p>快去购物吧！</p>
        </div>
      </div>
      <!--  收藏列表为空的时候显示的内容END -->
    </div>
  </div>
</template>

<script>
import Api from "@/api/Collect.js";
import { Notification } from "element-ui";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      collectList: [],
      isDelete: ""
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    delet(key) {
    this.isDelete = key
      
     
    },
    //   获取收藏数据
    getCollect() {
      // 获取收藏数据
      Api.getCollect({ user_id: this.$store.state.user_id })
        .then(res => {
          console.log(res.data);
          if (res.data.code == "001") {
            this.collectList = res.data.collectList;
          }
        })
        .catch(err => {
          return Promise.reject(err);
        });
    },
    //删除收藏数据
    async deleteCollect(id) {
      let { data: res } = await Api.deleteCollect({
        user_id: this.$store.state.user_id,
        product_id: id
      });
      console.log(res);
      if (res.code == "001") {
        Notification.success({ title: "删除", message: res.msg });
        this.collectList.forEach((item, index) => {
          if (item.product_id == id) {
            this.collectList.splice(index, 1);
          }
        });
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    //获取收藏数据
    this.getCollect();
    //   删除收藏数据
    this.deleteCollect();
    console.log(this.$store.state.user_id);
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
.collect {
  background-color: #f5f5f5;
  .collect-header {
    height: 64px;
    background-color: #fff;
    border-bottom: 2px solid #ff6700;
    .collect-title {
      width: 1225px;
      padding: 0 200px;
      height: 64px;
      line-height: 58px;
      font-size: 28px;
    }
  }
}

.collect .content {
  padding: 20px 70px;
  width: 1225px;
  margin: 0 auto;
  ul {
    width: 100%;
    // height: 880px;
    display: flex;
    flex-wrap: wrap;
    align-content: space-between;
    li:hover {
      transition: all 0.2s linear;
      box-shadow: #efefef 40px 40px 40px;
      margin-top: -4px;
      transition: all 0.3s linear;
    }

    li {
      position: relative;
      margin-bottom: 20px;
      background: white;
      width: 206px;
      height: 280px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-between;
      padding-bottom: 26px;
      margin-left: 20px;
      &:nth-of-type(1),
      &:nth-of-type(6),
      &:nth-of-type(11) {
        margin-left: 0;
      }
      i {
        position: absolute;
        right: 10px;
        top: 10px;
        cursor: pointer;
      }
      img {
        width: 100%;
        height: 160px;
      }
      p {
        &:nth-of-type(1) {
          font-size: 13px;
          width: 200px;
          text-align: center;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        &:nth-of-type(2) {
          font-size: 12px;
          color: #b0b0b0;
        }
        &:nth-of-type(3) {
          span {
            &:nth-of-type(1) {
              color: red;
              font-size: 16px;
              margin-right: 10px;
            }
          }
          s {
            color: #b0b0b0;
            font-size: 16px;
          }
        }
      }
    }
  }
}
.collect .content .goods-list {
  margin-left: -13.7px;
  overflow: hidden;
}
/* 收藏列表为空的时候显示的内容CSS */
.collect .collect-empty {
  width: 1225px;
  margin: 0 auto;
}
.collect .collect-empty .empty {
  height: 300px;
  padding: 0 0 130px 558px;
  margin: 65px 0 0;
  background: url(../../../src/assets/imgs/cart-empty.png) no-repeat 124px 0;
  color: #b0b0b0;
  overflow: hidden;
}
.collect .collect-empty .empty h2 {
  margin: 70px 0 15px;
  font-size: 36px;
}
.collect .collect-empty .empty p {
  margin: 0 0 20px;
  font-size: 20px;
}
/* 收藏列表为空的时候显示的内容CSS END */
</style>